﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class Management_Danhsachnhanvien : System.Web.UI.UserControl
{

    class StaffInfo
    {
        public int ID
        {
            get;
            set;
        }
        public string Name
        {
            get;
            set;
        }
    }

    List<StaffInfo> listStaff;

    override protected void OnInit(EventArgs e)
    {
        listStaff = new List<StaffInfo>();
    }

    public void loadAllStaff()
    {
        loadStaffOf(-1);
    }

    public void setEnabled(bool enabled)
    {
        liNhanvien.Enabled = enabled;
    }

    public void loadStaffOf(int IDWard)
    {
        listStaff.Clear();
        SqlConnection conn = new SqlConnection(GlobalConstant.CONNECTION_STRING);
        conn.Open();
        string sql = "SELECT ID, FirstName, LastName FROM tbStaff ";
        if (IDWard != -1)
            sql += " WHERE IDWard=" + IDWard;
        sql += " ORDER BY FirstName";

        SqlCommand cmd = new SqlCommand(sql, conn);
        SqlDataReader reader = cmd.ExecuteReader();
        while (reader.Read())
        {
            StaffInfo di = new StaffInfo();
            di.ID = reader.GetInt32(0);
            di.Name = reader.GetString(1)+ " " + reader.GetString(2);            
            listStaff.Add(di);
            liNhanvien.Items.Add(new ListItem(di.Name,di.ID.ToString()));
        }
        reader.Close();
        conn.Close();
    }

    public void setSelectedStaffID(int StaffID)
    {
        liNhanvien.SelectedValue = StaffID.ToString();
    }

    public int getCurrentStaffID()
    {
        return Int32.Parse(liNhanvien.SelectedValue);
    }

    public string getCurrentStaffName()
    {
        return liNhanvien.SelectedItem.ToString();
    }

    public static int getIDWardFromIDStaff(int IDStaff)
    {
        int result=-1;
        SqlConnection conn = new SqlConnection(GlobalConstant.CONNECTION_STRING);
        conn.Open();
        string sql = "SELECT IDWard FROM tbStaff WHERE ID=" +IDStaff;
        SqlCommand cmd = new SqlCommand(sql, conn);
        SqlDataReader reader = cmd.ExecuteReader();
        if (reader.Read())
        {
            result = reader.GetInt32(0);
        }
        reader.Close();
        conn.Close();
        return result;
    }

}